import { createRouter, createWebHistory } from 'vue-router'
import Layout from '../components/Layout.vue'

// 导入页面组件
const LoginView = () => import('../views/LoginView.vue')
const DashboardView = () => import('../views/DashboardView.vue')
const UserListView = () => import('../views/UserListView.vue')
const UserGroupView = () => import('../views/UserGroupView.vue')
const ArticleListView = () => import('../views/ArticleListView.vue')
const VideoListView = () => import('../views/VideoListView.vue')
const ReminderTemplateView = () => import('../views/ReminderTemplateView.vue')
const TriggerRuleView = () => import('../views/TriggerRuleView.vue')
const UserAnalyticsView = () => import('../views/UserAnalyticsView.vue')
const SystemSettingsView = () => import('../views/SystemSettingsView.vue')
const LogsView = () => import('../views/LogsView.vue')

const routes = [
  {
    path: '/login',
    name: 'login',
    component: LoginView,
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    meta: { requiresAuth: true },
    children: [
      {
        path: 'dashboard',
        name: 'dashboard',
        component: DashboardView,
        meta: { title: '数据看板' }
      },
      {
        path: 'users',
        name: 'users',
        component: UserListView,
        meta: { title: '用户列表' }
      },
      {
        path: 'user-groups',
        name: 'userGroups',
        component: UserGroupView,
        meta: { title: '用户分组' }
      },
      {
        path: 'articles',
        name: 'articles',
        component: ArticleListView,
        meta: { title: '健康文章' }
      },
      {
        path: 'videos',
        name: 'videos',
        component: VideoListView,
        meta: { title: '教学视频' }
      },
      {
        path: 'reminder-templates',
        name: 'reminderTemplates',
        component: ReminderTemplateView,
        meta: { title: '提醒模板' }
      },
      {
        path: 'trigger-rules',
        name: 'triggerRules',
        component: TriggerRuleView,
        meta: { title: '触发规则' }
      },
      {
        path: 'user-analytics',
        name: 'userAnalytics',
        component: UserAnalyticsView,
        meta: { title: '用户分析' }
      },
      {
        path: 'system-settings',
        name: 'systemSettings',
        component: SystemSettingsView,
        meta: { title: '系统配置' }
      },
      {
        path: 'logs',
        name: 'logs',
        component: LogsView,
        meta: { title: '日志查看' }
      }
    ]
  },
  // 404页面
  {
    path: '/:pathMatch(.*)*',
    redirect: '/dashboard'
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = to.meta.title + ' - 健康管理系统'
  } else {
    document.title = '健康管理系统'
  }

  // 检查是否需要登录
  if (to.meta.requiresAuth !== false) {
    const token = localStorage.getItem('token')
    if (!token) {
      // 没有token，重定向到登录页
      return next({ path: '/login' })
    }
  }
  next()
})

export default router